<?php

namespace App\Transformers\Admin;

use League\Fractal\TransformerAbstract;

class LoginLogTransformer extends TransformerAbstract
{
    public $availableIncludes = ['user'];

    public function transform($log)
    {
        return [
            'id' => $log->id,
            'user_id' => $log->user_id,
            'ip' => $log->ip,
            'created_at' => $log->created_at->format('Y-m-d H:i:s')
        ];
    }

    public function includeUser($log)
    {
        return $this->item($log->user, new class extends TransformerAbstract {
            public function transform($user) {
                return [
                    'id' => $user->id ?? null,
                    'username' => $user->username ?? null
                ];
            }
        });
    }
}

